package com.factors.HuangHePing.AttendanceAvg;

import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class AttendanceReducer extends Reducer<Text, IntWritable, Text, FloatWritable> {

    private FloatWritable result = new FloatWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        int count = 0;

        // 使用 for-each 循环遍历所有值
        for (IntWritable val : values) {
            sum += val.get();
            count++;
        }

        if (count > 0) {
            float average = (float) sum / count;
            result.set(average);
            context.write(key, result);
        }
    }
}